const app = getApp()
import{getUrl}from "../../../utils/url";
let _this;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    msg:{},
    temp:false
  },
 user:{},
  onShow(){
       let user= wx.getStorageSync('user');
         this.setData({
           msg:user
         })
      },

      //保存修改的信息
      submit(){
         if(this.user.name==''||this.user.name==undefined){
            this.user.name=this.data.msg.name;
         }
         if(this.user.phone==''||this.user.phone==undefined){
         this.user.phone=this.data.msg.phone
       }
       if(this.user.zfb==''||this.user.zfb==undefined){
        this.user.zfb=this.data.msg.zfb
      }

       if(this.user.dphone==''||this.user.dphone==undefined){
       this.user.dphone=this.data.msg.dphone;
     }
     if(this.user.id==''||this.user.id==undefined){
     this.user.id=this.data.msg.id;
     }
      let user=this.user;
      user.avatarUrl=this.data.msg.avatarUrl;
        
   wx.request({
     url: getUrl()+'/user/infoUser',
     data:user,
     success: function(res) {
        wx.setStorageSync('user',res.data);
        wx.showToast({
          title: '修改成功',
        })
        wx.switchTab({
          url: '../mine',
        })
     }
   })
      },
 //动态绑定姓名
 msgName(e){
/*       console.log(this.data.msg);  */
       const{value}=e.detail;
       const{name}=e.target.dataset;
       if(name=='name'){
        this.user.name=value;
        }else if(name=='phone'){
          this.user.phone=value;
       }else if(name=='dphone'){
        this.user.dphone=value;   
       }else if(name=='zfb'){
         this.user.zfb=value;
       }
    
      },

  formSubmit(e){
    wx.showLoading({
      title: '加载中',
      task:true
    })
    let form = e.detail.value;
    form.id = wx.getStorageSync('user').id;
    
    if (e.detail.value.nick_name != '' && e.detail.value.phone != '' ){
      _this.upload(function(r){
        form.avatar_url = _this.data.msg.avatar_url;
        app.com.post('wx/user/update/wx', form, function (res) {
          wx.hideLoading()
          if (res.code == 200) {
            wx.showToast({
              title: '修改成功',
            })
            let uinfo = wx.getStorageSync("user");
            uinfo.nick_name = e.detail.value.nick_name;
            wx.setStorageSync("user", uinfo)
            _this.setData({
              temp:false
            })
          }
        })
      })
      
    }else{
      wx.showToast({
        title: '修改失败',
        icon:'none'
      })
    }
  },
  changeAvatar(){/* 
    wx.chooseImage({
      count:1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function(res) {
        wx.compressImage({
          src: res.tempFilePaths[0], // 图片路径
          quality: 50, 
          success(res){
            _this.setData({
              'msg.avatar_url': res.tempFilePath,
              temp: true
            })
          },
          fail(res){
            console.log(res)
          }
        })
        // _this.setData({
        //   'msg.avatar_url': res.tempFilePaths[0],
        //   temp: true
        // })
        
      },
    }) */


   /*  wx.chooseImage({
      success: ret => {
        const tempFilePaths = ret.tempFilePaths
        console.log(tempFilePaths[0])
        wx.uploadFile({
        header:{
              "Authorization":"rXbyWY1J47s7omalrf97F8jEoK7WYg6c",
               'content-type': 'application/x-www-form-urlencoded;',
          },
          name: 'smfile',
          filePath: tempFilePaths[0],
          url: 'https://sm.ms/api/v2/upload',
          success: res => {
            console.log('上传成功：', res);
          }
        });
      }
    }) */

    wx.chooseImage({
      success (res) {
        const tempFilePaths = res.tempFilePaths[0]
        const id=wx.getStorageSync('user').id;
        wx.uploadFile({
          url: getUrl()+`/user/updoladfile?id=${id}`, 
          filePath: tempFilePaths,
          name:'file',
          header: {
            'Content-Type': 
             'json'
            },
          success (res){
            //this.data.msg.avatarUrl
               let msg=_this.data.msg;
               msg.avatarUrl=getUrl()+res.data;
            _this.setData({
              msg
            })
          }
        })   }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    _this= this
    this.setData({
      msg:wx.getStorageSync("user")
    })
  },
})